﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using ProjManagSolution.pmMyWebService;


/* 
 * What's left?
 * Get user by email -> user id
 * Validation info
 * Update info for user
 */
namespace ProjManagSolution
{
    public partial class Profile : ChildWindow
    {
        pmWebServiceClient ws = new pmWebServiceClient();

        user currentUser = new user();
        
        //protected string userEmail = Session.GetUserInfo();
        //protected int userID = 0;
       
        public Profile(user curUser)
        {
            InitializeComponent();

            //get the user
            currentUser = curUser;

            //update it in the box
            txtblokEmail.Text = currentUser.user_email;
            txtboxFirstName.Text = currentUser.user_firstname;
            txtboxLastName.Text = currentUser.user_lastname;
            txtboxPhoneNumber.Text = currentUser.user_phone;
            txtboxAddress.Text = currentUser.user_address;

            datepickBirthday.SelectedDate = Convert.ToDateTime(currentUser.user_birthday.ToString());
            if (currentUser.user_sex == 0)
                comboSex.SelectedIndex = 1;
            else if (currentUser.user_sex == 1)
                comboSex.SelectedIndex = 2;
            else
                comboSex.SelectedIndex = 0;
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if(Input_Validate())
                this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        protected bool Input_Validate()
        {
            //check password!
            if (passboxOldPassword.Password != "")
            {
                if (passboxOldPassword.Password != currentUser.user_password)
                {
                    MessageBox.Show("The old password is not correct! Leave it blank if you want to keep the old one");
                    return false;
                }
                //check passconfirm!
                else if (passboxNewPassword.Password == "")
                {
                    MessageBox.Show("Please enter your new password");
                    return false;
                }
                else if (passboxNewPassword.Password != passboxConfirmPassword.Password)
                {
                    MessageBox.Show("Confirm password does not match!");
                    return false;
                }
            }

            return true;
        }
        private void buttUpdate_Click(object sender, RoutedEventArgs e)
        {
            /*
            if (Input_Validate())
            {
                ws.UpdateUserDetailsCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_UpdateUserDetailsCompleted);

                user inputUser = new user();
                inputUser.user_password = passboxNewPassword.Password;
                inputUser.user_firstname = txtboxFirstName.Text;
                inputUser.user_lastname = txtboxLastName.Text;
                inputUser.user_birthday = datepickBirthday.SelectedDate;

                ws.UpdateUserDetailsAsync(txtblokEmail.Text, inputUser);
            }
            */
            if (Input_Validate())
                this.DialogResult = true;
        }
        

        private void buttCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

